package com.example.controller;

import com.example.annotations.Valid;
import com.example.entity.Student;
import com.example.exceptions.MyException;
import com.example.result.MSG;
import com.example.result.Result;
import com.example.result.ResultData;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.Map;

/**
 * @Author: chen
 * @CreateTime: 2022-08-16
 * @Version: 1.0
 */
@RestController
@RequestMapping("/hello")
public class HelloController {

    @GetMapping("/index")
    public Result index() {
        Map<String,String> data = new HashMap<>();
        data.put("name","张三");
        data.put("sex","男");
        return ResultData.success(data);
    }

    // 测试统一异常
    @GetMapping("/error")
    public Result error() {
        MyException.display(MSG.SYSTEM_ERROR);
        return ResultData.success();
    }

    // 除零异常
    @GetMapping("/server")
    public Result serverError() {
        int i = 1 / 0;
        return ResultData.success();
    }

    // 校验参数
    @PostMapping("/params")
    public Result verifyParams(@Valid @RequestBody Student student) {
        return ResultData.success(student);
    }

}
